package com.itheima.response;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

@WebServlet("/response/demo04")
public class ServletDemo04 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Refresh","3;URL=/response/demo04");
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("d://美女.jpg"));

        ServletOutputStream os = resp.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(os);
        int len;
        byte[] bytes=new byte[1024];
        while ((len=bis.read(bytes))!=-1){
            bos.write(bytes,0,len);
        }

        bis.close();
       //bos.close();

        //resp.setHeader("Refresh","3;URL=/response/demo04");
       /* //resp.setDateHeader("Expires",System.currentTimeMillis()+3600*1000);
        String news = "您的用户名或密码错误，3秒后自动跳转到登录页面...";

        //设置编码格式
        resp.setContentType("text/html;charset=UTF-8");
        //写出数据
        resp.getWriter().write(news);*/

        //设置响应消息头定时刷新
        //注意路径

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
